package com.xiewenyu.web.servlets;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Properties;

public class HelloServlet extends HttpServlet {

//    @Override
//    public void init(ServletConfig config) throws ServletException {
//        super.init(config);
//        System.out.println(this.getServletConfig().getInitParameter("username"));
//    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //System.out.println(servletContext.hashCode());
        super.service(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        super.doGet(req, resp);
        //设置响应编码
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");


        PrintWriter writer = resp.getWriter();
        writer.println("hello 你号");

        System.out.println(this.getServletConfig().getInitParameter("username"));

        //获取ServletContext
        ServletContext context = this.getServletContext();
        System.out.println(context.getInitParameter("url"));
        Enumeration<String> initParameterNames = context.getInitParameterNames();
        if(initParameterNames.hasMoreElements()){
            System.out.println(initParameterNames.nextElement());
//            writer.println();
        }
//        System.out.println(context.hashCode());
//        System.out.println(context.getContextPath());
//
//        //1获取ServletContext的配置参数
//        writer.println("context"+context.getInitParameter("url"));
//        writer.println(context.getInitParameterNames());
//
//        //3 共享数据
//        //String username = (String) context.getAttribute("username");
//        Object username = context.getAttribute("username");
//
////        //2请求转发
////        context.getRequestDispatcher("/hello2").forward(req,resp);
//
//        //读取配置信息
//        InputStream is = context.getResourceAsStream("WEB-INF/classes/db.properties");
//        Properties props=new Properties();
//        props.load(is);
//        String server = props.getProperty("server");
//        String port = props.getProperty("port");
//
//        writer.println("server2--"+server+":"+port);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
